package practice;

// 编写代码, 基于 CountDownLatch 模拟跑步比赛的过程

import java.util.concurrent.CountDownLatch;

public class Main4 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch runners = new CountDownLatch(6);  // 6名运动员
        for (int i = 0; i < 6; i++) {
            Thread t = new Thread(() -> {
                System.out.println("运动员出发！"+Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("运动员到达！"+Thread.currentThread().getName());

                // 撞线之后都要调用方法！
                runners.countDown();
            });
            t.start();
        }

        // 进行线程阻塞，等待所有的选手/线程都完成
        runners.await();

        System.out.println("全部选手一定到达 比赛结束！");
    }
}
